import SimpleTextEditor from '../simple-text-editor.js'
import {BaseWidget} from './base-widget.js'
import {WIDGET_TYPE} from './widget-const.js';

export default class TextWidget extends BaseWidget {
    constructor(sectionData, dispatcher) {
        super(sectionData, dispatcher);
        this._editor = undefined;
        this._inputFn = (code) => {
            this.textareaText = code;
            this._dirty = true;
        };
    }
    get sectionData() {
        this._sectionData.type = WIDGET_TYPE.TEXT;
        return this._sectionData;
    }
    get language() {return 'plaintext';}
    get textareaText() {return this._sectionData.content ?? '';}
    set textareaText(value) {this._sectionData.content = value;}
    Init() {
        super.Init();
        this._editContainerElem.setAttribute('data-language', 'text');
        return this;
    }
    Deinit() {
        if (this._editor) {
            this._editor.Deinit();
            this._editor = undefined;
        }
        super.Deinit();
    }
    _UpdateEdit() {
        if (!this._editor) {
            this._editor = new SimpleTextEditor();
            this._editContainerElem.append(this._editor.layoutElem);
            this._editor.layoutElem.innerText = this.textareaText;
            this._editor.Init(this.language, this._inputFn);
        }
    }
    _UpdateView() {
        this._viewContainerElem.innerText = this.textareaText;
    }
};

